package com.htzos.admin.system.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.htzos.admin.system.bean.SystemMenuBase;
import com.htzos.admin.system.bean.SystemRoleBase;
import com.htzos.admin.system.bean.SystemRoleMenu;
import com.htzos.admin.system.service.SystemMenuBaseService;
import com.htzos.admin.system.service.SystemRoleBaseService;
import com.htzos.admin.system.service.SystemRoleMenuService;
import com.htzos.base.parent.ctrl.BaseController;
import com.htzos.common.tool.ExtQueryBean;

@Controller
@RequestMapping("/systemRoleMenu")
public class SystemRoleMenuController extends BaseController{

    @Resource
    SystemRoleMenuService systemRoleMenuService;
    @Resource
    SystemMenuBaseService systemMenuBaseService;
    @Resource
    SystemRoleBaseService systemRoleBaseService;

    @RequestMapping(value="/list", method=RequestMethod.POST)
    public String list(HttpServletRequest request,SystemRoleMenu condition, ExtQueryBean extQuery) {
    	SystemRoleBase systemRoleBase = systemRoleBaseService.systemRoleBaseGet(condition.getRoleId());
    	
    	request.setAttribute("tableName", "修改权限 - " + systemRoleBase.getRoleName());
    	
        List<SystemRoleMenu> list= systemRoleMenuService.systemRoleMenuViewList(condition, extQuery);
        request.setAttribute("list", list);
        request.setAttribute("condition", condition);
        request.setAttribute("extQuery", extQuery);
        request.setAttribute("roleId", condition.getRoleId());

        request.getSession(true).setAttribute("conditionStr", condition);
		request.getSession(true).setAttribute("extQueryStr", extQuery);
        
        return "admin/system/systemRoleMenuList";
    }
    
    @RequestMapping(value="/list", method=RequestMethod.GET)
    public String listGet(HttpServletRequest request, SystemRoleMenu condition, ExtQueryBean extQuery, @RequestParam(value="s", required=false, defaultValue="0")Integer useSession) {
    	if(useSession == 1){
    		//使用Session里面的参数，通常是从update和insert跳转来的
    		HttpSession session = request.getSession();
    		if(session != null){
    			condition = (SystemRoleMenu) session.getAttribute("conditionStr");
    			extQuery = (ExtQueryBean) session.getAttribute("extQueryStr");
    		}
    	}

    	return list(request, condition, extQuery);
    }

    @RequestMapping("/delete")
    public ModelAndView delete(HttpServletRequest request, SystemRoleMenu systemRoleMenu) {
        systemRoleMenuService.systemRoleMenuDelete(systemRoleMenu);
        return new ModelAndView(new RedirectView("list"), "s", 1);
    }
    
    @RequestMapping("/setRight")
    public void setRight(HttpServletResponse response, int roleId, int menuId, String right, boolean checked) {
		systemRoleMenuService.systemRoleMenuSetRight(roleId, menuId, right, checked);

		try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html");
            PrintWriter out = null;
            out = response.getWriter();
            out.print("success");
            out.close();
        } catch (IOException e) {
        	e.printStackTrace();
        }
	}
}